import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86183
 * Date: 2024-01-04
 * Time: 20:03
 */
public class MyArrayList implements IList{
    private int[] myArrayList ;
    private int usedSize;

    public MyArrayList() {
        this.myArrayList =new int[10];
    }

    public MyArrayList(int usedSize) {

        this.myArrayList =new int[usedSize];
    }

    public int getUsedSize() {
        return usedSize;
    }

    public void setUsedSize(int usedSize) {
        this.usedSize = usedSize;
    }

    @Override
    public void add(int data) {
            if(isFull()){
                myArrayList= Arrays.copyOf(myArrayList,myArrayList.length*2);
            }
            myArrayList[usedSize]=data;
            usedSize++;
    }

    @Override
    public void add(int pos, int data) {
        if(pos<0||pos>usedSize){
            System.out.println("pos位置错误"+pos);
        }
        if(isFull()){
            myArrayList= Arrays.copyOf(myArrayList,myArrayList.length*2);
        }
        for (int i = usedSize-1; i >=pos ; i--) {
            myArrayList[i+1]=myArrayList[i];
        }
        myArrayList[pos]=data;
        usedSize++;
    }

    @Override
    public boolean contains(int toFind) {
        for (int i = 0; i < usedSize; i++) {
            if(myArrayList[i]==toFind){
                return true;
            }
        }
        return false;
    }

    @Override
    public int indexOf(int toFind) {
        for (int i = 0; i < usedSize; i++) {
            if(myArrayList[i]==toFind){
                return i;
            }
        }
        return -1;
    }

    @Override
    public int get(int pos) {
        if(pos<0||pos>=usedSize){
            System.out.println("pos位置错误"+pos);
        }
        if(isEmpty()){
            System.out.println("顺序表为空");
        }
        return myArrayList[pos];
    }

    @Override
    public void set(int pos, int value) {
        if(pos<0||pos>=usedSize){
            System.out.println("pos位置错误"+pos);
        }
        if(isEmpty()){
            System.out.println("顺序表为空");

        }
        myArrayList[pos]=value;
    }

    @Override
    public void remove(int key) {
        if(isEmpty()){
            System.out.println("顺序表为空");
            return;
        }
        int tmp=0;
        int i = 0;
        for (; i < usedSize; i++) {
            if(myArrayList[i]==key){
                tmp=i;
                break;
            }
        }
        if(i==usedSize){
            System.out.println("删除元素不存在");
            return;
        }
        for (int j = tmp; j < usedSize-1; j++) {
                myArrayList[j]=myArrayList[j+1];
        }
            usedSize--;


    }

    @Override
    public int size() {
       return this.getUsedSize();
    }

    @Override
    public void clear() {
        usedSize=0;

    }

    @Override
    public void display() {
        for (int i = 0; i < usedSize; i++) {
            System.out.print(myArrayList[i]+" ");
        }
    }

    @Override
    public boolean isFull() {
        if(usedSize==myArrayList.length) {
            return true;
        }
        return false;

    }

    @Override
    public boolean isEmpty() {
        if(usedSize==0){
            return true;
        }
        return false;
    }
}
